package cn.cupk.corporateshuttlemanagesystem.controller;

import cn.cupk.corporateshuttlemanagesystem.mapper.LineInformationMapper;
import cn.cupk.corporateshuttlemanagesystem.mapper.ReservationInformationMapper;
import cn.cupk.corporateshuttlemanagesystem.mapper.UserCancelStatsMapper;
import cn.cupk.corporateshuttlemanagesystem.pojo.Routes;
import cn.cupk.corporateshuttlemanagesystem.pojo.UserCancelStats;
import cn.cupk.corporateshuttlemanagesystem.result.PageResult;
import cn.cupk.corporateshuttlemanagesystem.result.RequestParams;
import cn.cupk.corporateshuttlemanagesystem.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ydxx")
@Slf4j
public class ReservationInformationController {

    @Autowired
    private ReservationInformationMapper reservationInformationMapper;

    @Autowired
    private LineInformationMapper lineInformationMapper;

    @Autowired
    private UserCancelStatsMapper userCancelStatsMapper;

    @PostMapping("page")
    public Result<Object> page(@RequestBody RequestParams<Routes> params) {
        String name = null;
        if (params.getData() != null)
            name = params.getData().getName();
        Page<Object> page = PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Routes> routes = lineInformationMapper.page(name);
        // 遍历查询结果
        List<Routes> routes1 = new ArrayList<>();
        for (Routes route : routes) {
            // 获取线路的站点名称
            String stopsId = route.getStopsId();

            String[] stationName = stopsId.split("，");
            List<Integer> stationId = new ArrayList<>();
            for (String s : stationName) {
                // 将 s 转换为 Integer 类型
                stationId.add(Integer.parseInt(s.trim()));
            }
            List<String> list = lineInformationMapper.stopList(stationId);
            route.setStationName(list);

            String[] split = route.getUserId().split("，");
            // 判断 split 中是否包含 params.getData().getUserId()
            if (split.length > 0) {
                boolean isOrder = true;
                for (String s : split) {
                    // 如果包含则不显示
                    if (s.equals(params.getData().getUserId())) {
                        isOrder = false;
                        break;
                    }

                }
                // 如果不包含则显示
                if (!isOrder) {
                    routes1.add(route);
                }
            }
        }
        return Result.ok(new PageResult<>(page.getTotal(), routes1));
    }

    @GetMapping("/cancel")
    public Result<Object> cancel(Integer routeId, String userId) {
        // 查询用户取消次数
        UserCancelStats stats = userCancelStatsMapper.selectByUserId(userId);
        int cancelCount = stats.getCancelCount();

        // 检查是否已经取消三次
        if (cancelCount >= 3) {
            return Result.error("您已经取消预约三次，不能再进行取消操作。");
        }

        // 查询路线信息
        Routes routes = lineInformationMapper.selectByRouteId(routeId);
        String[] split = routes.getUserId().split("，");

        // 遍历 split 数组，删除匹配的 userId
        for (int i = 0; i < split.length; i++) {
            if (split[i].equals(userId)) {
                split[i] = null;  // 找到匹配的 userId，置为 null 表示删除
            }
        }

        // 构建新的预约用户列表字符串
        StringBuilder sb = new StringBuilder();
        for (String s : split) {
            if (s != null) {
                sb.append(s).append("，");  // 将非空的 userId 添加到字符串中，并用逗号分隔
            }
        }

        // 将 StringBuilder 转换为最终的预约用户列表字符串
        String updatedUserIds = sb.toString();

        // 如果字符串长度大于 0，去除末尾逗号
        if (updatedUserIds.length() > 0) {
            updatedUserIds = updatedUserIds.substring(0, updatedUserIds.length() - 1);
        }

        // 更新数据库中的预约信息
        Integer result = lineInformationMapper.updateOrder(updatedUserIds, routeId);

        // 更新用户取消次数
        stats.setCancelCount(cancelCount + 1);
        userCancelStatsMapper.update(stats);

        // 返回操作结果
        return Result.ok(result > 0 ? "取消成功" : "取消失败");
    }

}





























